/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmUpdateClinic.java
 *
 * Created on Jun 23, 2011, 10:13:46 AM
 */
package khmhappointmentsystem;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jdesktop.application.Action;

/**
 *
 * @author mbelisle
 */
public class FrmUpdateClinic extends javax.swing.JInternalFrame
{

    static final Logger logger = Logger.getLogger(FrmNewAppointment.class.getName());
    private Vector<Vector<String>> data;
    private Vector<String> header;
    ArrayList<String> specialist = new ArrayList<String>();
    ArrayList<String> clinic = new ArrayList<String>();
    ArrayList<String> limit = new ArrayList<String>();
    FrmClinic frmClinic = null;

    /** Creates new form FrmUpdateClinic */
    public FrmUpdateClinic() throws Exception
    {

        //get data from databse
        data = Environment.getClinic();

        //Create Header for table
        header = new Vector<String>();
        header.add("Name");
        header.add("Specialist");
        header.add("Limit");

        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        tblclinic = new javax.swing.JTable();
        cmdClose = new javax.swing.JButton();
        cmdUpdate = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtSpecialist = new javax.swing.JTextField();
        txtClinic = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtLimit = new javax.swing.JTextField();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance().getContext().getResourceMap(FrmUpdateClinic.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        tblclinic.setModel(new javax.swing.table.DefaultTableModel(
            data,header
        ));
        tblclinic.setName("tblclinic"); // NOI18N
        tblclinic.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tblclinicMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tblclinic);

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance().getContext().getActionMap(FrmUpdateClinic.class, this);
        cmdClose.setAction(actionMap.get("Close")); // NOI18N
        cmdClose.setText(resourceMap.getString("cmdClose.text")); // NOI18N
        cmdClose.setName("cmdClose"); // NOI18N

        cmdUpdate.setAction(actionMap.get("Update")); // NOI18N
        cmdUpdate.setText(resourceMap.getString("cmdUpdate.text")); // NOI18N
        cmdUpdate.setName("cmdUpdate"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        txtSpecialist.setText(resourceMap.getString("txtSpecialist.text")); // NOI18N
        txtSpecialist.setName("txtSpecialist"); // NOI18N

        txtClinic.setText(resourceMap.getString("txtClinic.text")); // NOI18N
        txtClinic.setName("txtClinic"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        txtLimit.setText(resourceMap.getString("txtLimit.text")); // NOI18N
        txtLimit.setName("txtLimit"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGap(27, 27, 27)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(txtClinic, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE)
                            .addComponent(txtSpecialist, javax.swing.GroupLayout.DEFAULT_SIZE, 143, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(txtLimit, javax.swing.GroupLayout.DEFAULT_SIZE, 35, Short.MAX_VALUE)
                        .addGap(112, 112, 112))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(119, 119, 119)
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(169, 169, 169))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmdUpdate, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmdClose, javax.swing.GroupLayout.PREFERRED_SIZE, 161, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGap(8, 8, 8)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)))
                        .addGap(33, 33, 33))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 254, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtSpecialist, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4)
                    .addComponent(txtLimit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtClinic, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cmdClose)
                    .addComponent(cmdUpdate))
                .addGap(13, 13, 13))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-442)/2, (screenSize.height-422)/2, 442, 422);
    }// </editor-fold>//GEN-END:initComponents

    private void tblclinicMouseClicked(java.awt.event.MouseEvent evt)//GEN-FIRST:event_tblclinicMouseClicked
    {//GEN-HEADEREND:event_tblclinicMouseClicked
        // TODO add your handling code here:
        txtSpecialist.setText(tblclinic.getValueAt(tblclinic.getSelectedRow(), 0).toString());
        txtClinic.setText(tblclinic.getValueAt(tblclinic.getSelectedRow(), 1).toString());
        txtLimit.setText(tblclinic.getValueAt(tblclinic.getSelectedRow(), 2).toString());
    }//GEN-LAST:event_tblclinicMouseClicked

    @Action
    public void Close()
    {
        this.dispose();
    }

    @Action
    public void Update()
    {
        String name = txtClinic.getText();
        String doctor = txtSpecialist.getText();
        String total = txtLimit.getText();

        int n = JOptionPane.showConfirmDialog(null,
                "Are you sure you want to Update- " + tblclinic.getValueAt(tblclinic.getSelectedRow(), 0)
                + "?", "", JOptionPane.YES_NO_CANCEL_OPTION);
        //user click cancel
        if (n == 2)
        {
            return;
        }
        //yes
        if (n == 0)
        {
            try
            {
                DefaultTableModel model = new DefaultTableModel();
                model = (DefaultTableModel) tblclinic.getModel();
                int viewRow = tblclinic.getSelectedRow();
                int modelRow = tblclinic.convertRowIndexToModel(viewRow);
                //SQl statement to update row from the database
                String query = "Update clinic SET name = '" + doctor + "', specialist = '" + name + "', limit = '" + total + "' Where name = '" + tblclinic.getModel().getValueAt(modelRow, 0)
                        + "'";
                PreparedStatement pstmt = Environment.getDbConn().prepareStatement(query);

                pstmt.executeUpdate();
                JOptionPane.showMessageDialog(rootPane, "Specialist Clinic successfully updated.");
                this.dispose();

            }
            catch (Exception e)
            {
                String message = "An error occurred while updating specialist clinic.";
                logger.log(Level.SEVERE, message, e);

            }
        }
        //no
        if (n == 1)
        {
            return;
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmdClose;
    private javax.swing.JButton cmdUpdate;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tblclinic;
    private javax.swing.JTextField txtClinic;
    private javax.swing.JTextField txtLimit;
    private javax.swing.JTextField txtSpecialist;
    // End of variables declaration//GEN-END:variables
}
